<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-pic"></i>
          三方链接
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
        <el-select class="handle-input mr10" v-model="search" placeholder="订单状态" @change="changeType(search)">
          <el-option v-for="item in tradeStatus" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
        <el-button type="primary" icon="el-icon-lx-add" @click="showBox()">添加三方链接</el-button>
      </div>
      <el-table :data="tableData" border class="table" ref="multipleTable" header-cell-class-name="table-header" :row-style="{ height: '80px' }">
        <el-table-column prop="classifyPic" label="分类图标">
          <template #default="scope">
            <el-image class="table-td-thumb" :src="joinpic(scope.row.pictureUrl)"></el-image>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="名称"></el-table-column>
        <el-table-column prop="jumpUrl" label="跳转地址"></el-table-column>
        <el-table-column prop="createTime" label="创建时间" :formatter="dateFormat"></el-table-column>
        <el-table-column label="操作" width="250" align="center">
          <template #default="scope">
            <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button type="text" icon="el-icon-delete" class="red" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 编辑弹出框 -->
    <el-dialog title="创建用户" v-model="showAddUser" width="65%">
      <el-form ref="form" :model="list" label-width="100px">
        <el-form-item label="名称">
          <el-input v-model="list.name"></el-input>
        </el-form-item>
        <el-form-item label="跳转地址">
          <el-input v-model="list.jumpUrl"></el-input>
        </el-form-item>
        <el-form-item label="商品图片">
          <div class="crop-demo">
            <img v-if="list.pictureUrl" :src="joinpic(list.pictureUrl)" class="pre-img" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            <input class="crop-input" type="file" name="image" accept="image/*" @change="setImage" :disabled="isDisabled" />
          </div>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="showAddUser = false">取 消</el-button>
          <el-button v-if="isChange == 1" type="primary" @click="append">确 定</el-button>
          <el-button v-else type="primary" @click="edit">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { advertPicDel, advertPicAdd, advertPicList, advertPicEdit } from '../../api/system'
import { upload, pic } from '../../api/index'
import { formatDate } from '../../common/data'
import moment from 'moment'
export default {
  inject: ['reload'],
  filters: {
    formatDate(time) {
      time = time * 1000
      let date = new Date(time)
      console.log(new Date(time))
      return formatDate(date, 'yyyy-MM-dd hh:mm')
    },
  },
  name: 'basetable',
  data() {
    return {
      search: 4,
      tableData: [],
      showAddUser: false,
      list: {
        pictureType: 2,
      },
      isChange: '',
      fileList: '',
      picList: [],
      dialogVisible: false,
      hideUploadCard: false,
      tradeStatus: [
        {
          value: 4,
          label: '三方链接',
        },
      ],
    }
  },
  created() {
    this.getData()
  },
  mounted() { },
  methods: {
    dateFormat: function (row, column) {
      var date = row[column.property]
      if (date == undefined) {
        return ''
      }
      return moment(date).format('YYYY-MM-DD HH:mm:ss')
    },
    joinpic(item) {
      return `https://mall.xiyibaomall.com/imgs/${item}`
    },
    //预览大图
    picPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    //多图上传
    uploadFileMethod(param) {
      let formData = new FormData()
      if (param.file) {
        formData.append('file', param.file)
      } else {
        formData.append('file', param)
      }
      upload(formData).then((res) => {
        this.fileList = res.data[0].fileName
      })
    },
    // 编辑操作
    handleEdit(index, row) {
      this.picList = []
      let data = JSON.parse(JSON.stringify(row))
      this.list = data
      this.showAddUser = true
      this.isChange = 0
    },
    //修改分类
    edit() {
      this.showAddUser = false
      advertPicEdit(this.list).then((res) => {
        if (res.code === 2000) {
          this.$message.success('修改广告图成功')
          this.list = {}
          this.reload()
        } else {
          this.$message.error(res.message)
        }
      })
    },
    //上传图片
    setImage(file) {
      let imgFile = file.target.files[0]
      let formData = new FormData() //通过formdata上传
      formData.append('file', imgFile)
      upload(formData).then((res) => {
        this.list.pictureUrl = res.data[0].fileName
      })
    },
    //创建分类
    append() {
      this.list.parentId = this.$store.state.adminMsg.parentId
      this.list.pictureType = 4
      advertPicAdd(this.list).then((res) => {
        if (res.code === 2000) {
          this.$message.success('新增广告图成功')
          this.list = {}
          this.reload()
          this.showAddUser = false
        } else {
          this.$message.error(res.message)
        }
      })
    },
    // 获取分类列表
    getData() {
      advertPicList({
        parentId: this.$store.state.adminMsg.parentId,
        pictureType: this.search,
      }).then((res) => {
        this.tableData = res.data
      })
    },
    changeType() {
      this.getData()
    },
    //显示编辑框
    showBox() {
      this.showAddUser = true
      this.isChange = 1
    },

    // 触发搜索按钮
    handleSearch() {
      this.getData()
    },

    // 删除操作
    handleDelete(index, row) {
      // 二次确认删除
      let data = JSON.parse(JSON.stringify(row))
      this.$confirm('确定要删除吗？', '提示', {
        type: 'warning',
      })
        .then(() => {
          advertPicDel({
            id: data.id,
          }).then((res) => {
            if (res.code === 2000) {
              this.$message.success(res.message)
              this.reload()
            } else {
              this.$message.error(res.message)
            }
          })
        })
        .catch(() => { })
    },
  },
}
</script>

<style>
.hide .el-upload--picture-card {
  display: none;
}
</style>
<style lang="scss" scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}

.table {
  width: 100%;
  font-size: 14px;
}

.red {
  color: #ff0000;
}

.green {
  color: #0ab618;
}

.mr10 {
  margin-right: 10px;
}

.table-td-thumb {
  display: block;
  margin: auto;
  height: 60px;
  width: 60px;
}

.crop-demo {
  display: flex;
  align-items: flex-end;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  width: 148px;
  height: 148px;

  .pre-img {
    width: 146px;
    height: 146px;
    background: #f8f8f8;
    border: 1px solid #eee;
    border-radius: 5px;
    object-fit: cover;
  }

  .crop-input {
    position: absolute;
    width: 148px;
    height: 148px;
    left: 0px;
    top: 0px;
    opacity: 0;
    cursor: pointer;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 148px;
    height: 148px;
    line-height: 148px;
    text-align: center;
  }
}

.picPreview {
  width: 100%;
}
</style>
